package qlsl.androiddesign.view.subview.commonview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.qlsl.androiddesign.kuaiwa.R;

import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import qlsl.androiddesign.activity.commonactivity.MainActivity;
import qlsl.androiddesign.activity.commonactivity.TestActivity;
import qlsl.androiddesign.activity.subactivity.RecruitActivity;
import qlsl.androiddesign.activity.subactivity.RepairActivity;
import qlsl.androiddesign.activity.subactivity.ShoppingListActivity;
import qlsl.androiddesign.activity.subactivity.SignActivity;
import qlsl.androiddesign.activity.subactivity.TrailerActivity;
import qlsl.androiddesign.adapter.subadapter.ForumMenuAdapter;
import qlsl.androiddesign.adapter.subadapter.HomeBottomAdapter;
import qlsl.androiddesign.entity.otherentity.HomeBanner;
import qlsl.androiddesign.entity.otherentity.ThreeCategory;
import qlsl.androiddesign.fragment.commonfragment.FragmentTab1;
import qlsl.androiddesign.http.service.commonservice.HomeService;
import qlsl.androiddesign.library.pulltorefresh.PullToRefreshScrollView;
import qlsl.androiddesign.listener.OnDefaultRefreshListener;
import qlsl.androiddesign.view.baseview.FunctionView;
import qlsl.androiddesign.view.commonview.ChildViewPager;
import qlsl.androiddesign.view.commonview.ChildViewPager.OnSingleTouchListener;
import qlsl.androiddesign.view.indicatorview.AutoPlayManager;
import qlsl.androiddesign.view.indicatorview.ImageIndicatorView.OnItemClickListener;
import qlsl.androiddesign.view.indicatorview.IndicatorView;

public class TabView1 extends FunctionView<MainActivity> implements
		OnItemClickListener, OnSingleTouchListener {

	private FragmentTab1 fragment;

	private IndicatorView indicatorView;

	private AutoPlayManager autoBrocastManager;

	private GridView gridView;

	private GridView gridViewSmall;

	public TabView1(FragmentTab1 fragment, ViewGroup rootView,
			ViewGroup contentView) {
		super((MainActivity) fragment.getActivity());
		this.fragment = fragment;
		setContentView(rootView, contentView);
	}

	public void onTabResume() {
		((PullToRefreshScrollView) getContentView()).getRefreshableView()
				.smoothScrollTo(0, 0);
	}

	protected void initView(View view) {
		setTitle(activity.getResources().getString(R.string.app_name));
		setRightButtonResource(R.drawable.personal_h);
		showRightButton();

		RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
				ViewGroup.LayoutParams.WRAP_CONTENT,
				ViewGroup.LayoutParams.WRAP_CONTENT);
		lp2.addRule(RelativeLayout.CENTER_VERTICAL);

		TextView tv_city = (TextView) activity.getLayoutInflater().inflate(
				R.layout.common_backbutton_city, null);
		tv_city.setLayoutParams(lp2);
		((ViewGroup) findViewById(R.id.title_view)).addView(tv_city, 0);
		gridView = findViewById(R.id.gridView);
		gridViewSmall = findViewById(R.id.gridView_small);
		indicatorView = findViewById(R.id.net_indicate_view);
		indicatorView.setIndicateStyle(IndicatorView.INDICATE_ROUND_STYLE);
		indicatorView.setupLayoutByDefaultResource(1);
		indicatorView.show();
	}

	protected void initData() {
		startHomeImageManager();
		setGridViewData();
		HomeService.queryBannerList(this, fragment);
	}

	protected void initListener() {
		((PullToRefreshScrollView) getContentView())
				.setOnRefreshListener(new OnDefaultRefreshListener<ScrollView>());
		indicatorView.setOnItemClickListener(this);
		ChildViewPager viewPager = findViewById(R.id.viewpager_image);
		viewPager.setIntercept(true);
		viewPager.setOnSingleTouchListener(this);
	}

	@SuppressWarnings("unchecked")
	public <T> void showData(T... t) {
		if (t[0] instanceof List) {
			List<HomeBanner> list = (List<HomeBanner>) t[0];
			indicatorView.setTag(list);
			List<String> urls = new ArrayList<String>();
			for (HomeBanner item : list) {
				urls.add(item.getUrl());
			}
			indicatorView.setupLayout(urls);
			indicatorView.show();
		}
	}

	public <T> void showNoData(T... t) {

	}

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.grid_item:
			int pos = gridView.getPositionForView(view);

			switch (pos) {
			case 0:
				activity.setCurrentItem(1);
				break;
			case 1:
				activity.setCurrentItem(2);
				break;
			case 2:
				activity.setCurrentItem(3);
				break;
			case 3:
				activity.setCurrentItem(4);
				break;
			case 4:// 签到
				activity.startActivity(SignActivity.class);
				// activity.setCurrentItem(4);
				break;
			case 5:// 招聘
				activity.startActivity(RecruitActivity.class);
				// activity.setCurrentItem(4);
				break;
			case 6:// 快修
				activity.startActivity(RepairActivity.class);
				break;
			case 7:// 快拖 Repair
				activity.startActivity(TrailerActivity.class);
				// activity.setCurrentItem(4);
				break;
			}

			break;
		case R.id.list_item:
			startActivity(TestActivity.class);
			break;
		case R.id.grid_item_small:
			int posSmall = gridViewSmall.getPositionForView(view);

			switch (posSmall) {
			case 0://
				activity.setCurrentItem(1);
				break;
			case 1:
				activity.startActivity(SignActivity.class);
				break;
			case 2:
				activity.startActivity(RecruitActivity.class);
				break;
			case 3:
				activity.setCurrentItem(2);
				break;
			case 4:
				activity.setCurrentItem(3);
				break;
			case 5:
				activity.setCurrentItem(4);
				break;
			}

			break;

		case R.id.tv_city:
			// showToast("城市选择正在开发中");
			break;
		}

	}

	private void startHomeImageManager() {
		autoBrocastManager = new AutoPlayManager(indicatorView);
		autoBrocastManager.setBroadcastEnable(true);
		autoBrocastManager.setBroadCastTimes(Integer.MAX_VALUE);
		autoBrocastManager.loop();
	}

	protected void setGridViewData() {
		Integer[] imgs = new Integer[] { R.drawable.shopping_home,
				R.drawable.forum_home, R.drawable.assessment_home,
				R.drawable.car_home, R.drawable.sign_home,
				R.drawable.recruit_home, R.drawable.repair_home,
				R.drawable.trailer_home };
		String[] texts = new String[] { "商城", "论坛", "评估", "二手车", "签到", "招聘",
				"快修", "快拖" };
		List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
		for (int index = 0; index < imgs.length; index++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("icon", imgs[index]);
			map.put("text", texts[index]);
			list.add(map);
		}
		ForumMenuAdapter adapter = new ForumMenuAdapter(activity, list);
		gridView.setAdapter(adapter);

		// 底部设置
		Integer[] bottomImags = { R.drawable.shopping_h, R.drawable.wabi_h,
				R.drawable.zhaop_h, R.drawable.luntan_h, R.drawable.pingg_h,
				R.drawable.shic_h, };
		List<Integer> BottomList = new ArrayList<Integer>();
		for (int index = 0; index < bottomImags.length; index++) {
			BottomList.add(bottomImags[index]);
		}
		HomeBottomAdapter bottomAdapter = new HomeBottomAdapter(activity,
				BottomList);
		gridViewSmall.setAdapter(bottomAdapter);
	}

	@SuppressWarnings("unchecked")
	public void OnItemClick(View view, int position) {
		List<HomeBanner> list = (List<HomeBanner>) indicatorView.getTag();
		if (list != null && list.size() > position) {
			// HomeBanner item = list.get(position);
			// startActivity(item, BannerCategoryActivity.class);

			ThreeCategory.RowsBean rowsBean = new ThreeCategory.RowsBean();
			rowsBean.setPhoto("");
			rowsBean.flage = 1;
			rowsBean.setSumcierid("0");
			rowsBean.setSumciertype("");
			startActivity(rowsBean, ShoppingListActivity.class);
		}
	}

	public void onSingleTouch() {
		ChildViewPager viewPager = findViewById(R.id.viewpager_image);
		int position = viewPager.getCurrentItem();
		OnItemClick(null, position);
	}

}
